<div class="page-container">
    <div class="page-content">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="portlet light bordered">
                        <div class="portlet-title">
                            <div class="caption">
                              <span class="caption-subject font-green-sharp bold uppercase">
                              <i class="fa fa-gift"></i> 搜索条件
                              </span>
                            </div>
                        </div>
                        <div class="portlet-body form">
                            <form action="javascript:;" id="searchForm" class="form-horizontal">
                                <div class="form-body">
                                    <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
                                        <div class="form-group">
                                            <label class="col-md-4 control-label">产品名称：</label>
                                            <div class="col-md-8">
                                                <input class="form-control" type="text" id="productName" name="productName">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
                                        <div class="form-group">
                                            <label class="col-md-4 control-label">产品ID：</label>
                                            <div class="col-md-8">
                                                <input class="form-control" type="text" id="productId" name="productId">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="text-right">
                                        <button type="button" class="btn green" onclick="selectProductList()">查询
                                        </button>
                                        <button type="button" id="resetBtn" class="btn">重置
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="portlet light bordered">
                        <div class="portlet-title">
                            <div class="caption">
                              <span class="caption-subject font-green-sharp bold uppercase">
                              <i class="fa fa-gift"></i> 产品列表（选择一种产品创建订单）
                              </span>
                            </div>
                            <div class="actions btn-set text-right">
                                <button class="btn green btn-circle"
                                        onclick="addOrderCount()">
                                    <a class="fa fa-edit" style="text-decoration:none;color: #FFFFFF">填写预定数量</a>
                                </button>
                            </div>
                        </div>
                        <div class="portlet-body">
                            <table id="productTable"
                                   class="table table-striped table-bordered table-hover"
                                   data-toggle="table"
                                   data-method="post"
                                   data-content-type="application/x-www-form-urlencoded"
                                   data-query-params="queryParams"
                                   data-pagination="true"
                                   data-click-to-select="true"
                                   data-side-pagination="server"
                                   data-page-number="1"<#--第几页-->
                                   data-page-size="5"<#--每页数据条数-->
                                   data-page-list="[10, 20, 50, 200]"
                                   data-search-align = "left"
                            >
                                <thead>
                                <tr>
                                    <th data-radio="true" checkbox="true" ></th>
                                    <th data-field="product_id">产品编号</th>
                                    <th data-field="product_name">产品名称</th>
                                    <th data-field="author">作者</th>
                                    <th data-field="press" >出版社</th>
                                    <th data-field="type_name" >类别</th>
                                    <th data-field="price" >价格</th>
                                    <th data-field="stock">库存量</th>
                                </tr>
                                </thead>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<input class="hidden" id="customerId" name="customerId" value="${customerId}"/>
<script >
    jQuery(document).ready(function () {
        selectProductList();
        $("#resetBtn").on("click",function () {
            $("#productName").val("");
            $("#productId").val("");
        });
    });
    function queryParams(params) {
        params = $.extend({}, {state:'1'}, params);
        return getQueryParams(params, "searchForm");
    }
    function selectProductList() {
        $('#productTable').bootstrapTable("destroy");
        $('#productTable').bootstrapTable({
            method: "post",
            url: rootPath+"/productManage/ajax/getProductList"
        });
    }
    function addOrderCount() {
        var selections = $('#productTable').bootstrapTable('getSelections');
        if (selections.length == 0) {
            layer.msg('请选择一行数据进行操作！', function () {
            });
            return;
        }
        var productId = selections[0].product_id;
        var stock = selections[0].stock;
        var customerId = $("#customerId").val();
        layer.prompt({title:"请填入预订的数量(/个)：", formType: 3}, function(text, index){
            if (stock < text){
                commonJs.alertMsg("库存数量不足，请重新填写！",function () {
                    layer.close(index);
                })
            }else if (text < 1){
                commonJs.alertMsg("预定数量不可小于1，请重新填写！",function () {
                    layer.close(index);
                })
            }else {
                commonJs.ajaxRequest(rootPath+"/orderManage/ajax/createOrder",{
                    customerId:customerId,
                    productId:productId,
                    addCount:text
                },function(data){
                    if(data.code == '0000'){
                        commonJs.alertMsg("预定成功，如需预定其他书籍请继续选择。",function(){
                            $("#productTable").bootstrapTable("refresh");
                            layer.close(index);
                        })
                    }else {
                        layer.msg(data.msg);
                    }
                })
            }
        });
    }
</script>
